package com.demo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.demo.iodemo.FileReadDemo;
import com.demo.niodemo.NIOFileReadDemo;

/**
 * mvn compile exec:java -Dexec.mainClass="com.demo.CompareTest" -Dexec.args="d://a.txt d://log2.txt"
 * @author lixl
 *
 */
public class CompareTest {

	public static void main(String[] args) throws IOException {
		String fileName = "d://a.txt";
		File logFile = new File("d://log.txt");
		if(args!=null && args.length==2){
			fileName = args[0];
			logFile = new File(args[1]);
		}
		
		List<String> lines = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			long start = System.currentTimeMillis();
			FileReadDemo.readFileByFileInputStream(fileName);
			long end = System.currentTimeMillis();
			String line = "IOWRITE\t"+(i+1)+"\t"+(end-start)+"\n";
			System.out.println(line);
			lines.add(line);
		}
		for (int i = 0; i < 10; i++) {
			long start = System.currentTimeMillis();
			NIOFileReadDemo.readFileByRandomAccessFile(fileName);
			long end = System.currentTimeMillis();
			String line = "NIOWRITE\t"+(i+1)+"\t"+(end-start)+"\n";
			System.out.println(line);
			lines.add(line);
		}
		
		FileUtils.writeLines(logFile, lines, true);
	}
	
	

}
